package com.eightbears.beboy.ui.user.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.adapter.OrderListAdapter;
import com.eightbears.beboy.base.BaseFragment;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.dialog.CodeDialog;
import com.eightbears.beboy.base.view.qrcode.utils.CodeUtils;
import com.eightbears.beboy.entity.MyClientEntity;
import com.eightbears.beboy.entity.OrderEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.eightbears.beboy.ui.user.activity.InviteFriendsActivity;
import com.eightbears.beboy.ui.user.activity.LevelClientActivity;
import com.eightbears.beboy.ui.user.activity.OrderDetailActivity;
import com.socks.library.KLog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.CONNECTION_STATUS_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_USERCREATE_RELATION;

public class MyClientFragment extends BaseFragment {
    public static final String PARAMS_MY_CLIENT = "clientType";
    @BindView(R.id.ll_chat)
    LinearLayout mLlChat;
    @BindView(R.id.ll_info)
    LinearLayout mLlInfo;
    @BindView(R.id.ll_text1)
    LinearLayout mLlText1;
    @BindView(R.id.iv_icon1)
    ImageView mIvIcon1;
    @BindView(R.id.tv_client_right1)
    ImageView mTvClientRight1;
    @BindView(R.id.tv_add_num1)
    TextView mTvAddNum1;
    @BindView(R.id.ll_text_right1)
    LinearLayout mLlTextRight1;
    @BindView(R.id.rl_client_item1)
    RelativeLayout mRlClientItem1;
    @BindView(R.id.ll_text2)
    LinearLayout mLlText2;
    @BindView(R.id.iv_icon2)
    ImageView mIvIcon2;
    @BindView(R.id.tv_client_right2)
    ImageView mTvClientRight2;
    @BindView(R.id.tv_add_num2)
    TextView mTvAddNum2;
    @BindView(R.id.ll_text_right2)
    LinearLayout mLlTextRight2;
    @BindView(R.id.rl_client_item2)
    RelativeLayout mRlClientItem2;
    @BindView(R.id.ll_text3)
    LinearLayout mLlText3;
    @BindView(R.id.iv_icon3)
    ImageView mIvIcon3;
    @BindView(R.id.tv_client_right3)
    ImageView mTvClientRight3;
    @BindView(R.id.tv_add_num3)
    TextView mTvAddNum3;
    @BindView(R.id.ll_text_right3)
    LinearLayout mLlTextRight3;
    @BindView(R.id.rl_client_item3)
    RelativeLayout mRlClientItem3;
    @BindView(R.id.tv_s)
    TextView mTvS;
    @BindView(R.id.tv_m)
    TextView mTvM;
    @BindView(R.id.tv_l)
    TextView mTvL;
    @BindView(R.id.tv_add)
    TextView mTv_add;
    @BindView(R.id.tv_sum_client)
    TextView mTv_sum_client;

    private String clientParam;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_client, container, false);
        ButterKnife.bind(this, view);
        initView();
        initData();
        initEvent();
        return view;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    protected void initEvent() {
//        orderListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
//            @Override
//            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                orderInfo = (OrderEntity.OrderInfo) adapter.getData().get(position);
//                switch (view.getId()) {
//                    case R.id.shop_info_meal:
//                        startLocalActivity(OrderDetailActivity.class, PARAMS_ORDER_LIST, orderInfo);
//                        break;
//                    case R.id.tv_shopbook_mealbug:
//                        if (builder == null) {
//                            String code = "beboy://code/" + orderInfo.getCode();
//                            builder = new CodeDialog.Builder(getActivity());
//                            builder.setBitmap(CodeUtils.createImage(code, 150, 150, null));
//                            builder.setTitle(orderInfo.getCoupon().getTitle());
//                            builder.setTv_price("￥" + orderInfo.getCoupon().getPrice());
//                            builder.setTv_code(orderInfo.getCode() + "");
//                        }
//                        builder.create().show();
//                        break;
//
//                }
//            }
//        });
    }

    private void getData() {
        OkHttpUtils.get().url(URL_USERCREATE_RELATION)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .build().execute(new StringCallback() {
            @Override
            public void onResponse(String response) {
                MyClientEntity myClientEntity = GsonUtil.fromJson(response, MyClientEntity.class);
                if (CONNECTION_STATUS_SUCCESS.equals(myClientEntity.getStatus_code())) {
                    mTvS.setText(myClientEntity.getData().getS_children());
                    mTvM.setText(myClientEntity.getData().getM_children());
                    mTvL.setText(myClientEntity.getData().getL_children());
                    mTvAddNum1.setText(myClientEntity.getData().getNew_s_children());
                    mTvAddNum2.setText(myClientEntity.getData().getNew_m_children());
                    mTvAddNum3.setText(myClientEntity.getData().getNew_l_children());
                    mTv_add.setText(myClientEntity.getData().getNew_children());
                    mTv_sum_client.setText(myClientEntity.getData().getTotal_children());
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                KLog.e(e.getMessage());
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(getActivity(), status_error);
            }
        });
    }

    @OnClick(R.id.rl_client_item1)
    void client_item1() {
        clientParam = getString(R.string.text_client_level_s);
        //推广
        startLocalActivity(LevelClientActivity.class, PARAMS_MY_CLIENT, clientParam);
    }

    @OnClick(R.id.rl_client_item2)
    void client_item2() {
        clientParam = getString(R.string.text_client_level_m);
        //推广
        startLocalActivity(LevelClientActivity.class, PARAMS_MY_CLIENT, clientParam);
    }

    @OnClick(R.id.rl_client_item3)
    void client_item3() {
        clientParam = getString(R.string.text_client_level_l);
        //推广
        startLocalActivity(LevelClientActivity.class, PARAMS_MY_CLIENT, clientParam);
    }
}
